1 00:00:00,610 --> 00:00:05,770 Welcome back to give our players the ability to open the locked door inside of our basement. 2 00:00:05,770 --> 00:00:08,230 We're going to have to give them a key, right? 3 00:00:08,230 --> 00:00:11,560 However, we don't want to make getting this key very easy. 4 00:00:11,560 --> 00:00:15,910 Instead, we want to have multiple different positions around the map that we are going to randomly 5 00:00:15,910 --> 00:00:17,200 place our key model at. 6 00:00:17,200 --> 00:00:20,830 That can be clicked to obtain the key to open the breaker room door. 7 00:00:20,830 --> 00:00:26,320 This way, players are forced to explore and venture around our basement map to look for this key, 8 00:00:26,320 --> 00:00:29,500 while also being chased by our Squidward AI monster. 9 00:00:29,500 --> 00:00:34,060 So, inside of server storage and our other stuff folder, we have a whole bunch of different positions 10 00:00:34,060 --> 00:00:35,830 around the map for our key. 11 00:00:35,830 --> 00:00:41,380 So as you can see, there is a position we have on a crate hidden back here. 12 00:00:41,380 --> 00:00:45,700 We have one and another spot behind our spam shelf. 13 00:00:45,700 --> 00:00:49,990 We have another spot and we are in pitch darkness. 14 00:00:49,990 --> 00:00:53,530 It is, uh, I believe this one's on the floor hidden behind some crates. 15 00:00:53,530 --> 00:00:57,400 So there's a lot of different hidden positions on the map that we're going to place this key at. 16 00:00:57,400 --> 00:01:02,170 We're going to take this breaker room key giver, which has a click part inside of it. 17 00:01:02,170 --> 00:01:07,630 And when that is clicked, we're going to give the breaker room key tool to that particular player. 18 00:01:08,020 --> 00:01:13,330 So inside of server script service we can open up our services and open up the door key service. 19 00:01:13,330 --> 00:01:18,340 And then we're also going to have to open up our game communication enum for the server. 20 00:01:18,610 --> 00:01:21,220 Inside of the door key service we'll need two services. 21 00:01:21,220 --> 00:01:23,140 One is going to be server storage. 22 00:01:26,110 --> 00:01:29,380 And the other service is going to be server script service. 23 00:01:32,990 --> 00:01:35,690 We can create the variable to represent this module script. 24 00:01:35,690 --> 00:01:38,300 So door key service is equal to a table. 25 00:01:38,300 --> 00:01:40,820 And we will return that at the end of our script. 26 00:01:42,620 --> 00:01:46,220 And then from this point we can make a reference to a few events. 27 00:01:46,220 --> 00:01:52,340 One is going to be the game Comms event or the Bindable, so we'll call it Game Comms Bindable which 28 00:01:52,340 --> 00:01:56,210 is in server storage, dot events, dot Bindesboll's dot game communication. 29 00:01:56,210 --> 00:02:00,560 Because we're going to have a script tell us when we should place the key randomly on the map. 30 00:02:00,560 --> 00:02:04,280 We're not going to do it instantly, but we're going to wait until we are told to do it. 31 00:02:04,280 --> 00:02:11,690 So we can also get our game comms enum and require that from the server script service, dot server, 32 00:02:11,690 --> 00:02:15,050 dot modules, dot enums, dot game communication. 33 00:02:15,890 --> 00:02:18,620 And then we're also going to get our anti-cheat module script. 34 00:02:18,620 --> 00:02:23,870 So we can require that from server script service, dot server dot modules, dot anti-cheat. 35 00:02:24,110 --> 00:02:28,070 We're going to get that folder that stores all of the different possible key positions. 36 00:02:28,070 --> 00:02:34,100 So we can call it key positions is equal to server storage dot other stuff dot key positions. 37 00:02:34,100 --> 00:02:36,650 And we'll get all of the children inside of that folder. 38 00:02:36,650 --> 00:02:41,360 We'll get the key tool itself which is in server storage, dot tools. 39 00:02:41,360 --> 00:02:43,100 And it's called the breaker room key. 40 00:02:43,100 --> 00:02:44,480 So we'll grab that. 41 00:02:44,480 --> 00:02:49,070 And then we're also going to get the giver for the tool or the thing that we need to click so we can 42 00:02:49,070 --> 00:02:50,510 call it key giver. 43 00:02:50,510 --> 00:02:54,980 And that's in server storage dot other stuff dot breaker room key giver. 44 00:02:54,980 --> 00:02:57,650 And then we're also going to need a random data type. 45 00:02:58,180 --> 00:03:03,160 Now we can go ahead and create a function for spawning the key somewhere. 46 00:03:03,160 --> 00:03:08,740 We would pass a part that represents the position, and then we would have to do the same math that 47 00:03:08,740 --> 00:03:14,860 we did inside of our money service, where we're going to have to adjust the key frame of our key giver 48 00:03:14,860 --> 00:03:18,580 to be flush with the surface of wherever this part is at. 49 00:03:19,230 --> 00:03:24,090 We're also going to need a function to listen for when our key giver is clicked, so we can call on 50 00:03:24,090 --> 00:03:24,810 clicked. 51 00:03:25,110 --> 00:03:28,920 And this is where we're also going to use our anti-cheat to make sure the player didn't grab this key 52 00:03:28,920 --> 00:03:30,210 from like across the map. 53 00:03:30,210 --> 00:03:33,540 So to this function, we would have to pass the click part. 54 00:03:33,540 --> 00:03:36,270 And the player that clicked our click detector. 55 00:03:36,510 --> 00:03:40,020 And then we can also have a function to listen to our game Combinable. 56 00:03:40,020 --> 00:03:45,300 So on game comms event and this will get passed in action. 57 00:03:45,450 --> 00:03:49,290 And all we'll need for this service is an initialize function. 58 00:03:49,290 --> 00:03:53,040 So door key service init. 59 00:03:53,040 --> 00:03:59,250 And all we're going to do is listen to our game comms Bindable event and connect our on game comms event 60 00:03:59,250 --> 00:04:00,030 function. 61 00:04:00,150 --> 00:04:05,550 So now we can go ahead and go to our Game Communication enum and create a section just for our door 62 00:04:05,550 --> 00:04:08,280 key service so we can do door key service. 63 00:04:09,040 --> 00:04:13,450 And the only action that we're going to need in here is to set up our key giver. 64 00:04:16,020 --> 00:04:22,860 So now we can check inside of our on game comms event function if our action is equal to the game comms 65 00:04:22,860 --> 00:04:28,650 enum dot, get the door key service and listen to the setup key giver action. 66 00:04:28,650 --> 00:04:35,970 If it is, then what we're going to do is we're going to generate a random number using RNG next integer 67 00:04:35,970 --> 00:04:39,840 from one to the max number of key positions. 68 00:04:39,840 --> 00:04:43,050 So we're just going to pick a random position inside of our table. 69 00:04:43,050 --> 00:04:47,850 And then what we could do is we could have our spawn key function or spawn key giver. 70 00:04:47,850 --> 00:04:54,060 When it places it on the map, it can return back to us the actual instance or model, and then we'll 71 00:04:54,060 --> 00:04:59,790 refer to that model and listen for when it is clicked, so we can have this return back to us a model. 72 00:04:59,790 --> 00:05:03,030 And we could get that model which would be the key giver. 73 00:05:03,030 --> 00:05:06,060 And we can call this key giver on map. 74 00:05:06,210 --> 00:05:09,180 And that's going to be equal to our spawn key function. 75 00:05:09,180 --> 00:05:15,090 And we would pass from our key positions table whatever part is at this particular index. 76 00:05:15,090 --> 00:05:20,160 And after we get this key giver inside of it, there is a part called our click part. 77 00:05:20,160 --> 00:05:23,550 And inside of the click part we have our click detector. 78 00:05:23,550 --> 00:05:26,400 And we're going to listen for when it is clicked. 79 00:05:26,400 --> 00:05:29,550 And we can connect a lambda function to it, get the player. 80 00:05:29,550 --> 00:05:32,670 And then we would call our on clicked function. 81 00:05:32,670 --> 00:05:38,310 We would pass the key giver dot click part and then the player itself. 82 00:05:38,700 --> 00:05:44,430 So to spawn our key giver on the map, what we need to do is we need to get a clone of our key giver. 83 00:05:44,430 --> 00:05:45,810 So we'll clone that. 84 00:05:45,810 --> 00:05:50,940 And then we're going to use the pivot two function on this model to change the key frame. 85 00:05:50,940 --> 00:05:53,820 And the key frame is going to be a key frame dot new. 86 00:05:53,820 --> 00:05:57,480 And we're going to get the part dot position. 87 00:05:57,480 --> 00:06:02,610 And you remember we need to subtract it by vector three dot new zero on the x. 88 00:06:02,610 --> 00:06:07,050 And then for the y we need to get the size of this part on the y axis divided by two. 89 00:06:07,080 --> 00:06:09,330 So we're flush with the surface. 90 00:06:12,440 --> 00:06:17,930 And then after that we're going to add a vector three dot new zero on the x axis. 91 00:06:17,930 --> 00:06:18,950 But on the y axis. 92 00:06:18,950 --> 00:06:26,030 This time we have to refer to our clone and get the extents size on the y axis and divide that by two. 93 00:06:26,060 --> 00:06:32,390 So we get the proper position that we need to set for our part to be flush with the surface and zero 94 00:06:32,390 --> 00:06:33,830 on the z axis. 95 00:06:35,100 --> 00:06:41,220 So once we get this C frame here, then we can go ahead and rotate our key randomly as well. 96 00:06:41,220 --> 00:06:46,860 So we'll multiply this by a C frame dot angles or add a keyframe dot angles. 97 00:06:46,860 --> 00:06:51,930 We don't care about the x axis, but we do want to rotate it randomly around the y axis. 98 00:06:51,930 --> 00:07:00,510 So we can use our RNG next integer anywhere from -360 to 360 degrees, and then zero on the z axis. 99 00:07:00,510 --> 00:07:04,200 And that's all we're going to need to do for our pivot two function. 100 00:07:04,920 --> 00:07:07,710 And it looks like I accidentally put this zero in the wrong spot. 101 00:07:07,740 --> 00:07:12,570 We have to move it outside of our Math.round function, so we'll pass zero right here. 102 00:07:12,570 --> 00:07:19,080 So we get zero on the x random rotation here on the y, and then zero rotation on the z. 103 00:07:19,740 --> 00:07:24,510 And then once we do that we're going to set the clone dot parent equal to the workspace dot Krusty Krab. 104 00:07:24,510 --> 00:07:26,070 And we'll just put it in the basement. 105 00:07:26,370 --> 00:07:29,250 And then we can go ahead and return our caregiver clone. 106 00:07:29,790 --> 00:07:33,840 And then once we get our key giver clone return back to here, we're going to listen for when it gets 107 00:07:33,840 --> 00:07:36,480 clicked and we're going to call our onClick function. 108 00:07:36,480 --> 00:07:38,820 What do you want to do when this function gets called? 109 00:07:38,820 --> 00:07:42,600 Well, first we want to check if the player who clicked the click part cheated or not. 110 00:07:42,600 --> 00:07:47,010 So we're going to get cheated and the message from our anti-cheat. 111 00:07:47,010 --> 00:07:50,100 And we're going to check the player distance to our object. 112 00:07:50,100 --> 00:07:52,710 We'll pass the player, we'll pass the click part. 113 00:07:52,710 --> 00:07:57,030 And then the distance is going to be the click part dot click detector. 114 00:07:58,620 --> 00:08:01,560 Dot max activation distance. 115 00:08:02,610 --> 00:08:07,920 If this player cheated, then we're going to kick them and give them a message. 116 00:08:07,920 --> 00:08:11,940 Returned back from our anti-cheat, and then we'll just return out of this function. 117 00:08:12,030 --> 00:08:16,920 Otherwise, we're going to check if the player already has the key in their inventory or they're already 118 00:08:16,920 --> 00:08:17,610 holding the key. 119 00:08:17,610 --> 00:08:24,540 So we can call a variable key on player, and we'll check the player's backpack and find first child 120 00:08:24,540 --> 00:08:28,500 and see if they have a breaker room key. 121 00:08:28,980 --> 00:08:35,790 Or we can check the player's character and find first child breaker room key. 122 00:08:36,480 --> 00:08:40,530 So if this player has a key on them, then we're just going to return as well. 123 00:08:40,530 --> 00:08:43,980 Otherwise we can go ahead and get a clone of our key. 124 00:08:46,070 --> 00:08:50,540 And then we can set the parent of our clone equal to the player dot backpack. 125 00:08:50,540 --> 00:08:51,350 Perfect. 126 00:08:51,620 --> 00:08:55,490 So now this means we can go ahead and open up our game sections module script. 127 00:08:55,490 --> 00:09:01,250 And when we go to start the second section of our game, this is when we could go ahead and set up the 128 00:09:01,250 --> 00:09:03,680 key giver or place it randomly in our basement. 129 00:09:03,680 --> 00:09:08,870 So after we have teleported all of the players into the basement and we've done all of the different 130 00:09:08,870 --> 00:09:13,520 setup or whatever, we can actually just insert it anywhere inside of here. 131 00:09:13,520 --> 00:09:16,070 So for example, we could put it right here. 132 00:09:16,070 --> 00:09:22,370 We'll refer to our game coms Bindable, and we'll fire and give the action in our game Coms enum on 133 00:09:22,370 --> 00:09:28,580 the server dot door key service, and tell it to set up the key giver on the map. 134 00:09:28,580 --> 00:09:29,360 Perfect. 135 00:09:29,630 --> 00:09:34,910 So now if we go ahead and play and wait till we get to section two, we should have our key giver randomly 136 00:09:34,910 --> 00:09:35,630 on the map somewhere. 137 00:09:35,630 --> 00:09:36,950 So let's go ahead and test it out. 138 00:09:41,790 --> 00:09:46,950 All right, so now that our basement section is initialized, we can go ahead and check to see if we 139 00:09:46,950 --> 00:09:50,670 got that key giver inside of the basement folder. 140 00:09:50,970 --> 00:09:53,370 And there it is our breaker room key giver. 141 00:09:53,370 --> 00:09:55,020 And it's all the way over there. 142 00:09:55,020 --> 00:10:01,020 So we can go ahead and grab our player here and try to migrate him over to wherever our key giver is 143 00:10:01,020 --> 00:10:01,650 at. 144 00:10:02,850 --> 00:10:06,660 So we'll go ahead and take a look again to see where it is exactly. 145 00:10:07,530 --> 00:10:09,300 So it's right over there. 146 00:10:09,840 --> 00:10:14,550 So we'll keep on moving our character to roughly wherever its position is at. 147 00:10:14,550 --> 00:10:16,680 I believe it's inside of here somewhere. 148 00:10:17,100 --> 00:10:18,780 And let's see if we can find it. 149 00:10:19,850 --> 00:10:20,780 There it is. 150 00:10:20,780 --> 00:10:22,700 So it appeared on top of this barrel. 151 00:10:22,700 --> 00:10:27,800 And as you can see inside of our key giver, we also have a little point light in there. 152 00:10:27,800 --> 00:10:33,080 It's very dim, but it's bright enough so you can actually see the key and it slightly illuminates its 153 00:10:33,080 --> 00:10:33,710 surroundings. 154 00:10:33,710 --> 00:10:36,620 So it's not impossible to see it in the dark. 155 00:10:36,620 --> 00:10:42,770 But now that we have this key giver, if we go back to the client here and we go ahead and grab it, 156 00:10:43,610 --> 00:10:48,050 unfortunately it doesn't look like we're actually able to get anything out of our key giver. 157 00:10:48,050 --> 00:10:50,060 So let's go ahead and figure out why. 158 00:10:50,450 --> 00:10:50,930 All right. 159 00:10:50,930 --> 00:10:55,700 And the reason why we weren't getting anything out of our key giver is because we have to change the 160 00:10:55,700 --> 00:10:56,960 variable reference here. 161 00:10:56,960 --> 00:11:02,150 This is thinking that we're referring to our global or basically our key giver right here, instead 162 00:11:02,150 --> 00:11:06,290 of the key giver or the cloned key giver that gets returned from our function. 163 00:11:06,290 --> 00:11:11,750 So we actually need to copy this and make that reference here and also make that reference here. 164 00:11:11,750 --> 00:11:14,840 That's why it wasn't working, but now should be working. 165 00:11:14,840 --> 00:11:17,180 So let's go ahead and try it out again okay. 166 00:11:17,180 --> 00:11:21,980 This time we can go ahead and check where our our breaker key giver spawned. 167 00:11:21,980 --> 00:11:29,780 So if we go into the Krusty Krab into the basement, it's all the way it seems over here. 168 00:11:29,780 --> 00:11:34,970 And it looks like we're going to have to actually, let's go ahead and copy the position and let's go 169 00:11:34,970 --> 00:11:37,880 ahead and move our player to that position. 170 00:11:37,880 --> 00:11:45,020 So if we close that out, go to our player and move the root part to this position. 171 00:11:46,270 --> 00:11:48,310 And then we can go back to the client. 172 00:11:50,350 --> 00:11:52,750 And here is our key line on the ground. 173 00:11:52,750 --> 00:11:55,210 As you can see you can kind of see it glowing here. 174 00:11:55,210 --> 00:11:56,530 But we can go ahead and click it. 175 00:11:56,800 --> 00:11:57,700 And there we go. 176 00:11:57,700 --> 00:11:59,470 We get our break room key. 177 00:11:59,470 --> 00:12:06,640 So now we're going to go ahead and have to find our way to the door to go ahead and, you know, open 178 00:12:06,640 --> 00:12:09,850 the breaker room so we can figure out where we are. 179 00:12:09,850 --> 00:12:10,390 Exactly. 180 00:12:10,390 --> 00:12:12,700 That would help, but I can't really see that well. 181 00:12:14,050 --> 00:12:16,060 But I think we are getting close. 182 00:12:16,060 --> 00:12:16,270 Yep. 183 00:12:16,300 --> 00:12:17,770 Should be right back here. 184 00:12:17,770 --> 00:12:18,370 Here we go. 185 00:12:18,370 --> 00:12:21,820 So if we interact with the door, we require the key breaker room key. 186 00:12:21,820 --> 00:12:23,920 So let's pull it out and open the door. 187 00:12:24,190 --> 00:12:25,150 And there we go. 188 00:12:25,150 --> 00:12:27,970 Now we are able to open the door and we've unlocked it with our key. 189 00:12:28,330 --> 00:12:30,310 And now the door is locked. 190 00:12:30,310 --> 00:12:31,120 Perfect. 191 00:12:31,270 --> 00:12:34,180 All right, so our door key service is working great. 192 00:12:34,180 --> 00:12:36,640 We're making excellent progress so far on this project. 193 00:12:36,640 --> 00:12:39,400 So let's keep it going and I'll see you in the next lecture.